package com.lg.algorithm.base;

import lombok.Setter;
import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * abc 循环打印十次
 */
public class OrderPrint {

    static class ThreadA extends Thread {
        Thread targetT;
        Integer count;
        ThreadA(Thread targetT,int n) {
            this.targetT = targetT;
            this.count = n;
        }

        @SneakyThrows
        @Override
        public void run() {
            //同步
            TimeUnit.SECONDS.sleep(1);
            for (int i = 0; i < count; i++) {
                System.out.println("-----"+(i+1)+"-----");
                System.out.println("a");
                LockSupport.unpark(targetT);
                LockSupport.park();
            }
        }
    }

    static class ThreadB extends Thread {
        Thread targetT;
        Integer count;
        ThreadB(Thread targetT,int n) {
            this.targetT = targetT;
            this.count = n;
        }

        @Override
        public void run() {
            for (int i = 0; i < count; i++) {
                LockSupport.park();
                System.out.println("b");
                LockSupport.unpark(targetT);
            }
        }
    }

    static class ThreadC extends Thread {
        @Setter
        Thread targetT;
        Integer count;
        ThreadC(Thread targetT,int n) {
            this.targetT = targetT;
            this.count = n;
        }

        @Override
        public void run() {
            for (int i = 0; i < count; i++) {
                LockSupport.park();
                System.out.println("c");
                LockSupport.unpark(targetT);
            }
        }
    }


    static class ThreadD extends Thread {
        @Setter
        Thread targetT;
        Integer count;
        String content;
        ThreadD(Thread targetT,int n,String content) {
            this.targetT = targetT;
            this.count = n;
            this.content = content;
        }

        @Override
        public void run() {
            for (int i = 0; i < count; i++) {
                LockSupport.park();
                System.out.println(content);
                LockSupport.unpark(targetT);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

//        ThreadC threadC = new ThreadC(null,10);
//        ThreadB threadB = new ThreadB(threadC,10);
//        ThreadA threadA = new ThreadA(threadB,10);
//
//        threadC.setTargetT(threadA);
//
//        threadB.start();
//        threadC.start();
//        threadA.start();

        ThreadD threadD1 = new ThreadD(null, 10,"a");
        ThreadD threadD3 = new ThreadD(threadD1, 10,"b");
        ThreadD threadD2 = new ThreadD(threadD3, 10,"c");

        threadD1.setTargetT(threadD2);

        threadD1.start();
        threadD2.start();
        threadD3.start();

        TimeUnit.SECONDS.sleep(1);
        LockSupport.unpark(threadD1);


    }
}
